//
//  MainHeaderView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/12.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "MainHeaderView.h"
#import <YBImageBrowser/YBImageBrowser.h>

@interface MainHeaderView ()<SDCycleScrollViewDelegate,YBImageBrowserDataSource>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) UILabel *titleLab;


@end

@implementation MainHeaderView

- (void)setupSubViews
{
    [self addSubview:self.cycleScrollView];
    
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.text = @"商品列表";
    self.titleLab.font = [UIFont boldSystemFontOfSize:16];
    self.titleLab.textColor = kColor333333;
    [self addSubview:self.titleLab];
}

- (void)setupConstraints
{
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.equalTo(@(140/375.0*kScreenW));
    }];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cycleScrollView.mas_bottom).offset(kWidth(20));
        make.left.equalTo(self).offset(kWidth(16));
    }];
}

- (void)setImageUrlStrings:(NSArray *)imageUrlStrings
{
    _imageUrlStrings = imageUrlStrings;
    _cycleScrollView.imageURLStringsGroup = self.imageUrlStrings;
}

- (void)browserImage:(NSInteger)index
{
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSource = self;
    browser.currentIndex = index;
    [browser show];
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    [self browserImage:index];
}

- (SDCycleScrollView *)cycleScrollView
{
    if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    }
    return _cycleScrollView;
}

#pragma mark - YBImageBrowserDataSource
- (NSUInteger)yb_numberOfCellForImageBrowserView:(YBImageBrowserView *)imageBrowserView {
    return self.imageUrlStrings.count;
}
- (id<YBImageBrowserCellDataProtocol>)yb_imageBrowserView:(YBImageBrowserView *)imageBrowserView dataForCellAtIndex:(NSUInteger)index {
    YBImageBrowseCellData *data = [YBImageBrowseCellData new];
    data.url = [NSURL URLWithString:self.imageUrlStrings[index]];
    data.sourceObject = self.cycleScrollView;
    return data;
}

@end
